package cn.com.wxd.util.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.action.AjaxResult;
import cn.com.wxd.service.freemarker.TemplateFactor;
import cn.com.wxd.service.freemarker.TemplateType;
import cn.com.wxd.service.javamail.AsyncMailSender;
import cn.com.wxd.service.sms.SmsSentFactor;
import cn.com.wxd.common.CharValidator;
import cn.com.wxd.common.DateUtil;

/**
 * Created by ComXu on 2017/5/22.
 */
public class RequestNotifyUtil {
    private static final Logger log = LoggerFactory.getLogger(RequestNotifyUtil.class);
    HttpServletRequest request;
    int interval = 60; //默认发送的时间间隔为60秒
    public RequestNotifyUtil(HttpServletRequest request){
        this.request = request;
    }
    /**
     *
     * @param request
     * @param interval 默认不容许重复发送的时间间隔，默认为60秒
     */
    public RequestNotifyUtil(HttpServletRequest request, int interval){
        this.request = request;
        if(interval < 0){
            interval = 0;
        }
        this.interval = interval;
    }
    /**
     * 短信发送
     *
     * @param sentphone    手机号
     * @param data         模板数据
     * @param tempLateName 模板名称
     */
    public AjaxResult sentSMS(String sentphone, HashMap<String, String> data,
                              String tempLateName) {
        try {
            if (sentphone == null || sentphone.trim().equals("") || !CharValidator.isMobile(sentphone)) { //手机号判断
                return AjaxResult.failureResult("手机号码不正确");
            } else {
                String timeStamp = (String) request.getSession().getAttribute("verphone" + sentphone);
                //从session中获取上次发送的时间
                String nowTimeStamp = DateUtil.getDatetimeStr();
                long timepoor = 0;
                try {
                    timepoor = DateUtil.getTimePoor(timeStamp, nowTimeStamp);
                } catch (Exception ignored) {
                }
                if (timeStamp == null || timepoor > interval) {   //判断距离上次发送的时间
                    String smscontent =
                            TemplateFactor.getTemplate(TemplateType.SMSTEMP).getTemplate(tempLateName, data);
                    //int re = SmsSentFactor.getSenter().sent(sentphone, smscontent);
                    //使用异步方式发送短信
                    int re = SmsSentFactor.getSenter().asyncSent(sentphone, smscontent);
                    if (re == 1) {
                        request.getSession().setAttribute("verphone" + sentphone, nowTimeStamp); //发送后存入session
                        return AjaxResult.successResult("短信发送成功");
                    } else {
                        return AjaxResult.failureResult("短信发送失败");
                    }
                } else {
                    return AjaxResult.failureResult("发送时间间隔不能超过" + interval + "秒");
                }
            }
        } catch (Exception e) {
            log.error("短信发送失败!" + e.getMessage(), e);
            return AjaxResult.failureResult("短信发送失败");
        }
    }

    /**
     * 邮件发送
     *
     * @param sentemail    邮箱地址
     * @param sunject      邮件主题
     * @param data         模板数据
     * @param tempLateName 模板名称
     */
    public AjaxResult sentEmail(String sentemail, String sunject,
                                 HashMap<String, String> data, String tempLateName) {
        try {
            if (sentemail == null || sentemail.trim().equals("") || !CharValidator.isEmail(sentemail)) {
                return AjaxResult.failureResult("邮箱地址不正确");
            } else {
                //从session中获取上次发送的时间
                String timeStamp = (String) request.getSession().getAttribute("veremail" + sentemail);
                String nowTimeStamp = DateUtil.getDatetimeStr();
                long timepoor = 0;
                try {
                    timepoor = DateUtil.getTimePoor(timeStamp, nowTimeStamp);
                } catch (Exception ignored) {
                }
                if (timeStamp == null || timepoor > interval) {   //判断距离上次发送的时间
                    String emailcontent = TemplateFactor
                            .getTemplate(TemplateType.EMAILTEMP).getTemplate(tempLateName, data);
                    AsyncMailSender ams = new AsyncMailSender();
                    //发送邮件了。
                    //boolean re = ms.send(sentemail, sunject, emailcontent);
                    boolean re = ams.send(sentemail, sunject, emailcontent);  //使用异步方式发送邮件
                    if (!re) {
                        return AjaxResult.failureResult("邮件发送失败");
                    } else {
                        request.getSession().setAttribute("veremail" + sentemail, nowTimeStamp); //发送后存入session
                        return AjaxResult.successResult("邮件发送成功");
                    }
                } else {
                    return AjaxResult.failureResult("邮件发送失败");
                }
            }
        } catch (Exception e) {
            log.error("邮件发送错误!" + e.getMessage(), e);
            return AjaxResult.failureResult("邮件发送失败");
        }
    }
}
